/*
 * Copyright (C) 2009 Otávio Scherer Garcia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.technorati4j.entity;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;

import com.googlecode.technorati4j.util.StringUtils;

/**
 * Response entity for search API.
 * 
 * @author Otávio Scherer Garcia
 * @version $Revision$
 * 
 */
public final class SearchResponse
    implements Response {

    /** Query string. */
    private String query;

    /** Number of matches. */
    private Integer queryCount;

    /** Duration of query. */
    private Double queryTime;

    /** Value of start parameter. */
    private Integer rankingStart;

    /**
     * List of items found.
     */
    private List<SearchResponseItem> items;

    @SuppressWarnings("unchecked")
    public SearchResponse(Element element) {
        Element result = element.getChild("result");
        query = result.getChildText("query");
        queryCount = StringUtils.toInteger(result.getChildText("querycount"));
        queryTime = StringUtils.toDouble(result.getChildText("querytime"));
        rankingStart = StringUtils.toInteger(result.getChildText("rankingstart"));

        items = new ArrayList<SearchResponseItem>();
        List<Element> _items = element.getChildren("item");
        for (Element item : _items) {
            this.items.add(new SearchResponseItem(item));
        }
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public Integer getQueryCount() {
        return queryCount;
    }

    public void setQueryCount(Integer queryCount) {
        this.queryCount = queryCount;
    }

    public Double getQueryTime() {
        return queryTime;
    }

    public void setQueryTime(Double queryTime) {
        this.queryTime = queryTime;
    }

    public Integer getRankingStart() {
        return rankingStart;
    }

    public void setRankingStart(Integer rankingStart) {
        this.rankingStart = rankingStart;
    }

    public List<SearchResponseItem> getItems() {
        return items;
    }

    public void setItems(List<SearchResponseItem> items) {
        this.items = items;
    }

    @Override
    public String toString() {
        final StringBuilder str = new StringBuilder();
        str.append("query=");
        str.append(this.query);
        str.append("&queryCount=");
        str.append(this.queryCount);
        str.append("&queryTime=");
        str.append(this.queryTime);
        str.append("&rankingStart=");
        str.append(this.rankingStart);
        str.append("&items=");
        str.append(this.items);

        return str.toString();
    }

}
